#!/bin/bash

# findrs - find files that have lines with trailing whitespace
# Copyright © 2000-2009 by Pádraig Brady <P@draigBrady.com>.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
# See the GNU General Public License for more details,
# which is available at www.gnu.org


# Notes:
#
# Could have option also to flag files that have many consequtive blank lines
#   the corresponding regexp would be? "^[ 	]*${2,}"

script_dir=$(dirname "$0")              #directory of this script
script_dir=$(readlink -f "$script_dir") #Make sure absolute path

. "$script_dir"/supprt/fslver

Usage() {
	ProgName=$(basename "$0")
	echo "find Redundant whiteSpace.
Usage: $ProgName [-w] [-t[#]] [-c] [[-r] [-f] paths(s) ...]

-w enables mode to report whitespace at the end of lines.
This is the default mode if none specified.

-t enables mode to report erroneous mixing of indenting
spaces and tabs (on a single line).
If a number is passed to -t it sets the width of the tabs,
which allows for more thorough checking.

If -c specified then the number of lines in each file,
with problematic whitespace is reported, in addition
to the file names. Note this will take longer.

If --view specified then the erroneous whitespace found
is highlighted using vim.

If no path(s) specified then the currrent directory is assumed."
	exit
}

#Note in the following grep expression there's a space and tab in []
#and the second [] contains a CR so it works for DOS files
eol='[ 	]+[]*$'

for arg
do
	case "$arg" in
	-c)
		count="yes" ;;
	--view)
		view="yes" ;;
	-w)
		eol_specified="yes" ;;
	-t*)
		#keep only last 2 digits
		ts=$(echo $arg|tr -cd '[:digit:]'|sed 's/^0*//;s/.*\(..\)/\1/')
		[ -z "$ts" ] && ts=8 #very rare to have larger than this
		at_start='^[ 	]*'
		tabs="$at_start( 	|	 {$ts,})" ;;
	-h|--help|-help)
		Usage ;;
	-v|--version)
		Version ;;
	*)
		argsToPassOn="$argsToPassOn $(shell_quote "$arg")" ;;
	esac
done

if [ -z "$eol_specified" ] && [ "$tabs" ]; then
	re="$tabs"
elif [ "$tabs" ] ; then
	re="$tabs|$eol"
else
	re="$eol"
fi

. "$script_dir"/supprt/getfpf "$argsToPassOn"

find "$@" -type f -size +0c ! -name "*$LF*" -printf "$FPF\0" |
sort -zu | #merge files (indirectly) specified multiple times
xargs -r0 file |
sed -n 's/\(.*\):[^:]*text.*/\1/p' | #assumes ':' not in desc
tr '\n' '\0' |
if [ "$view" = "yes" ]; then
	xargs -r0 grep -nE "$re" |
	"$script_dir"/supprt/rmlint/view_ws.sh -
elif [ "$count" = "yes" ]; then
	xargs -r0 grep -Ec "$re" |
	grep -vE "(:0$|^0$)" |
	sort -k2,2nr -t:
else
	xargs -r0 grep -El "$re"
fi
